﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FluentValidation;
using Microhard.WarehouseInsp.Buz.Models;
using Microhard.WarehouseInsp.Buz.Msg;
using FluentValidation.Results;
using Microhard.WarehouseInsp.Buz.Interfaces;
using Microhard.Core;

namespace Microhard.WarehouseInsp.Buz.Validator
{
    public class SupProjectModelValidator : AbstractValidator<SupProjectModel>
    {
        private ISupProject isp = IoC.Resolve<ISupProject>();

        public SupProjectModelValidator()
        {
            RuleSet("Insert", () =>
            {
                RuleFor(u => u.Name).NotEmpty().WithMessage(SubProjectMsg.NeedName).When(r => r.OperationStatus == Core.DBUtility.OperationStatus.Add);
                RuleFor(u => u.Name).Length(1, 50).WithMessage(SubProjectMsg.MaxNameLength).When(r => r.OperationStatus == Core.DBUtility.OperationStatus.Add);
                RuleFor(u => u.Credit).GreaterThan(0).WithMessage(SubProjectMsg.NeedCredit).When(r => r.OperationStatus == Core.DBUtility.OperationStatus.Add);
                RuleFor(u => u.SupervisionFee).GreaterThan(0).WithMessage(SubProjectMsg.NeedSupervisionFee).When(r => r.OperationStatus == Core.DBUtility.OperationStatus.Add);

                RuleFor(u => u.PedgeeCompanyName).NotEmpty().WithMessage(SubProjectMsg.NeedPedgeeCompanyName).When(r => r.OperationStatus == Core.DBUtility.OperationStatus.Add);
                RuleFor(u => u.PedgeeCompanyName).Length(1, 50).WithMessage(SubProjectMsg.MaxPedgeeCompanyName).When(r => r.OperationStatus == Core.DBUtility.OperationStatus.Add);

                RuleFor(u => u.PedgeeContactor).NotEmpty().WithMessage(SubProjectMsg.NeedPedgeeContactor).When(r => r.OperationStatus == Core.DBUtility.OperationStatus.Add);
                RuleFor(u => u.PedgeeContactor).Length(1, 50).WithMessage(SubProjectMsg.MaxPedgeeContactor).When(r => r.OperationStatus == Core.DBUtility.OperationStatus.Add);

                RuleFor(u => u.PedgeeContactorPhone).NotEmpty().WithMessage(SubProjectMsg.NeedPedgeeContactorPhone).When(r => r.OperationStatus == Core.DBUtility.OperationStatus.Add);
                RuleFor(u => u.PedgeeContactorPhone).Length(1, 50).WithMessage(SubProjectMsg.MaxPedgeeContactorPhone).When(r => r.OperationStatus == Core.DBUtility.OperationStatus.Add);

                RuleFor(u => u.Code).NotEmpty().WithMessage(SubProjectMsg.NeedCode).When(r => r.OperationStatus == Core.DBUtility.OperationStatus.Add);
                RuleFor(u => u.Code).Length(1, 50).WithMessage(SubProjectMsg.MaxCode).When(r => r.OperationStatus == Core.DBUtility.OperationStatus.Add);


                RuleFor(u => u.PMName).NotEmpty().WithMessage(SubProjectMsg.NeedPM).When(r => r.OperationStatus == Core.DBUtility.OperationStatus.Add);
                RuleFor(u => u.ProtalEmpName).NotEmpty().WithMessage(SubProjectMsg.NeedProtalName).When(r => r.OperationStatus == Core.DBUtility.OperationStatus.Add);

                RuleFor(u => u.MortgagorContractorCellphone).NotEmpty().WithMessage(SubProjectMsg.NeedBorrowerContractorCellphone).When(r => r.OperationStatus == Core.DBUtility.OperationStatus.Add);
                RuleFor(u => u.MortgagorContractorCellphone).Length(1, 50).WithMessage(SubProjectMsg.MaxBorrowerContractorCellphon).When(r => r.OperationStatus == Core.DBUtility.OperationStatus.Add);

                RuleFor(u => u.MortgagorContractorName).NotEmpty().WithMessage(SubProjectMsg.NeedBorrowerContractorName).When(r => r.OperationStatus == Core.DBUtility.OperationStatus.Add);
                RuleFor(u => u.MortgagorContractorName).Length(1, 50).WithMessage(SubProjectMsg.NeedBorrowerContractorName).When(r => r.OperationStatus == Core.DBUtility.OperationStatus.Add);

                // RuleFor(u => u.MortgagorFRCellphone).NotEmpty().WithMessage(SubProjectMsg.NeedBorrowerFRCellphone).When(r => r.OperationStatus == Core.DBUtility.OperationStatus.Add);
                RuleFor(u => u.MortgagorFRCellphone).Length(0, 50).WithMessage(SubProjectMsg.MaxBorrowerFRCellphone).When(r => r.OperationStatus == Core.DBUtility.OperationStatus.Add);

                RuleFor(u => u.MortgagorFRIdentityNo).NotEmpty().WithMessage(SubProjectMsg.NeedBorrowerFRIdentityNo).When(r => r.OperationStatus == Core.DBUtility.OperationStatus.Add);
                RuleFor(u => u.MortgagorFRIdentityNo).Length(1, 50).WithMessage(SubProjectMsg.MaxBorrowerFRIdentityNo).When(r => r.OperationStatus == Core.DBUtility.OperationStatus.Add);

                RuleFor(u => u.MortgagorFRName).NotEmpty().WithMessage(SubProjectMsg.NeedBorrowerFRName).When(r => r.OperationStatus == Core.DBUtility.OperationStatus.Add);
                RuleFor(u => u.MortgagorFRName).Length(1, 50).WithMessage(SubProjectMsg.MaxBorrowerFRName).When(r => r.OperationStatus == Core.DBUtility.OperationStatus.Add);

                RuleFor(u => u.Rate).GreaterThan(0).WithMessage(SubProjectMsg.NeedRate).When(r => r.OperationStatus == Core.DBUtility.OperationStatus.Add);

                Custom(u =>
                {
                    if (isp.IsDuplicateName(new SupProjectModel
                    {
                        ProjectID = u.ProjectID,
                        Name = u.Name
                    }))
                    {
                        return new ValidationFailure("Name", SubProjectMsg.NameIsDuplicate);
                    }
                    if (isp.IsDuplicateCode(new SupProjectModel
                    {
                        ProjectID = u.ProjectID,
                        Code = u.Code
                    }))
                    {
                        return new ValidationFailure("Code", SubProjectMsg.CodeIsDuplicate);
                    }
                    if (string.IsNullOrEmpty(u.MortgagorID))
                    {
                        return new ValidationFailure("MortgagorName", SubProjectMsg.NeedMortgagorID);
                    }
                    if (u.PrjEndTime <= u.PrjStartTime)
                    {
                        return new ValidationFailure("PrjEndTime", SubProjectMsg.EndBiggerThanStart);
                    }
                    if (string.IsNullOrEmpty(u.SalerID))
                    {
                        return new ValidationFailure("SalerName", SubProjectMsg.NeedSalerName);
                    }
                    if (u.PrjType == Guid.Empty)
                    {
                        return new ValidationFailure("PrjType", SubProjectMsg.NeedPrjType);
                    }
                    return null;
                });
            });

            RuleSet("Update", () =>
            {
                RuleFor(u => u.Name).NotEmpty().WithMessage(SubProjectMsg.NeedName);
                RuleFor(u => u.Name).Length(1, 50).WithMessage(SubProjectMsg.MaxNameLength);
                RuleFor(u => u.Credit).GreaterThan(0).WithMessage(SubProjectMsg.NeedCredit);
                RuleFor(u => u.SupervisionFee).GreaterThan(0).WithMessage(SubProjectMsg.NeedSupervisionFee);

                RuleFor(u => u.PedgeeCompanyName).NotEmpty().WithMessage(SubProjectMsg.NeedPedgeeCompanyName);
                RuleFor(u => u.PedgeeCompanyName).Length(1, 50).WithMessage(SubProjectMsg.MaxPedgeeCompanyName);

                RuleFor(u => u.PedgeeContactor).NotEmpty().WithMessage(SubProjectMsg.NeedPedgeeContactor);
                RuleFor(u => u.PedgeeContactor).Length(1, 50).WithMessage(SubProjectMsg.MaxPedgeeContactor);

                RuleFor(u => u.PedgeeContactorPhone).NotEmpty().WithMessage(SubProjectMsg.NeedPedgeeContactorPhone);
                RuleFor(u => u.PedgeeContactorPhone).Length(1, 50).WithMessage(SubProjectMsg.MaxPedgeeContactorPhone);

                RuleFor(u => u.Code).NotEmpty().WithMessage(SubProjectMsg.NeedCode);
                RuleFor(u => u.Code).Length(1, 50).WithMessage(SubProjectMsg.MaxCode);

                RuleFor(u => u.PMName).NotEmpty().WithMessage(SubProjectMsg.NeedPM);
                RuleFor(u => u.ProtalEmpName).NotEmpty().WithMessage(SubProjectMsg.NeedProtalName);

                RuleFor(u => u.MortgagorContractorCellphone).NotEmpty().WithMessage(SubProjectMsg.NeedBorrowerContractorCellphone);
                RuleFor(u => u.MortgagorContractorCellphone).Length(1, 50).WithMessage(SubProjectMsg.MaxBorrowerContractorCellphon);

                RuleFor(u => u.MortgagorContractorName).NotEmpty().WithMessage(SubProjectMsg.NeedBorrowerContractorName);
                RuleFor(u => u.MortgagorContractorName).Length(1, 50).WithMessage(SubProjectMsg.NeedBorrowerContractorName);

                //RuleFor(u => u.MortgagorFRCellphone).NotEmpty().WithMessage(SubProjectMsg.NeedBorrowerFRCellphone);
                RuleFor(u => u.MortgagorFRCellphone).Length(0, 50).WithMessage(SubProjectMsg.MaxBorrowerFRCellphone);

                RuleFor(u => u.MortgagorFRIdentityNo).NotEmpty().WithMessage(SubProjectMsg.NeedBorrowerFRIdentityNo);
                RuleFor(u => u.MortgagorFRIdentityNo).Length(1, 50).WithMessage(SubProjectMsg.MaxBorrowerFRIdentityNo);

                RuleFor(u => u.MortgagorFRName).NotEmpty().WithMessage(SubProjectMsg.NeedBorrowerFRName);
                RuleFor(u => u.MortgagorFRName).Length(1, 50).WithMessage(SubProjectMsg.MaxBorrowerFRName);

                RuleFor(u => u.Rate).GreaterThan(0).WithMessage(SubProjectMsg.NeedRate);

                Custom(u =>
                {
                    if (isp.IsApproving(u.ProjectID))
                    {
                        return new ValidationFailure("Name", SubProjectMsg.IsApproving);
                    }
                    if (isp.IsDuplicateName(new SupProjectModel
                    {
                        ProjectID = u.ProjectID,
                        Name = u.Name
                    }))
                    {
                        return new ValidationFailure("Name", SubProjectMsg.NameIsDuplicate);
                    }
                    if (isp.IsDuplicateCode(new SupProjectModel
                    {
                        ProjectID = u.ProjectID,
                        Code = u.Code
                    }))
                    {
                        return new ValidationFailure("Code", SubProjectMsg.CodeIsDuplicate);
                    }
                    if (u.PrjEndTime <= u.PrjStartTime)
                    {
                        return new ValidationFailure("PrjEndTime", SubProjectMsg.EndBiggerThanStart);
                    }
                    if (string.IsNullOrEmpty(u.SalerID))
                    {
                        return new ValidationFailure("SalerName", SubProjectMsg.NeedSalerName);
                    }
                    if (u.PrjType == Guid.Empty)
                    {
                        return new ValidationFailure("PrjType", SubProjectMsg.NeedPrjType);
                    }
                    return null;
                });
            });
        }
    }
}
